Redis : Redis database client

更新时间:
2024-05-14
下载文档

Redis : Redis database client

redis provides a Redis client that can easily connect to the Redis server.

Example

const iosched = require('iosched');
const redis = require('redis');
const client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.on('error', function(error) {
  console.error(error);
});

client.set('key', 'value', redis.print);
client.get('key', redis.print);

while (true) {
  iosched.poll();
}

Commands

This library is a 1 to 1 mapping of the Redis commandsopen in new window.

Each Redis command is exposed as a function on the client object. All functions take either an args array plus optional callback function or a variable number of individual arguments followed by an optional callback. Examples:

client.hmset('key', [{'foo': 'bar'}], function(err, res) {
  // ...
});

// The same as
client.hmset('key', {'foo': 'bar'}, function(err, res) {
  // ...
});

// Or
client.hmset('key', 'foo', 'bar', function(err, res) {
  // ...
});

Care should be taken with user input if arrays are possible (via body-parser, query string or other method), as single arguments could be unintentionally interpreted as multiple args.

If the key is missing, reply will be null. Only if the Redis Command Referenceopen in new window states something else it will not be null.

client.get('missing_key', function(err, reply) {
  // reply is null when the key is missing
  console.log(reply);
});

Minimal parsing is done on the replies. Commands that return a integer return JavaScript Numbers, arrays return JavaScript Array. HGETALL returns an Object keyed by the hash keys. All strings will either be returned as string or as buffer depending on your setting. Please be aware that sending null, undefined and Boolean values will result in the value coerced to a string!

User can use the following code to import the redis module.

var redis = require('redis');

Support

The following shows redis module APIs available for each permissions.

 User ModePrivilege Mode
redis.createClient
redis.print
redis.add_command
client.auth
client.quit
client.end
client.hgetall
client.hmset
client.publish
client.subscribe
client.unsubscribe
client.multi
client.exec
client.exec_atomic
client.batch
client.monitor
client.server_info
client.duplicate
client.send_command
client.connected
client.command_queue_length

Redis Class

redis.createClient()

createClient() returns a RedisClient object. createClient() accepts these arguments:

  • redis.createClient([options])
    • options {Object} See options details.
    • returns: {Redis}
  • redis.createClient(redis_url[, options])
    • redis_url {String} Redis url.
    • options {Object} See options details.
    • returns: {Redis}
  • redis.createClient(port[, host][, options])
    • port {Integer} Port.
    • host {String} Host.
    • options {Object} See options details.
    • returns: {Redis}

options properties

PropertyDefaultDescription
host127.0.0.1IP address of the Redis server
port6379Port of the Redis server
urlnullThe URL of the Redis server. Format: [redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]
string_numbersnullSet to true, Redis will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above Number.MAX_SAFE_INTEGER === 2^53). Hiredis is incapable of this behavior, so setting this option to true will result in the built-in javascript parser being used no matter the value of the parser option.
return_buffersfalseIf set to true, then all replies will be sent to callbacks as Buffers instead of Strings.
detect_buffersfalseIf set to true, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas return_buffers applies to every command on a client. NOTICE: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers.
socket_keepalivetrueIf set to true, the keep-alive functionality is enabled on the underlying socket.
socket_initial_delay0Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis.
no_ready_checkfalseWhen a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will not respond to any commands. To work around this, Node Redis has a "ready check" which sends the INFO command to the server. The response from the INFO command indicates whether the server is ready for more commands. When ready, redis emits a ready event. Setting no_ready_check to true will inhibit this check.
enable_offline_queuetrueBy default, if there is no active connection to the Redis server, commands are added to a queue and are executed once the connection has been established. Setting enable_offline_queue to false will disable this feature and the callback will be executed immediately with an error, or an error will be emitted if no callback is specified.
retry_unfulfilled_commandsfalseIf set to true, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. incr). This is especially useful if you use blocking commands.
passwordnullIf set, client will run Redis auth command on connect. Alias auth_pass NOTICE: Node Redis < 2.5 must use auth_pass
dbnullIf set, client will run Redis select command on connect.
disable_resubscribingfalseIf set to true, a client won't resubscribe after disconnecting.
rename_commandsnullPassing an object with renamed commands to use instead of the original functions. For example, if you renamed the command KEYS to "DO-NOT-USE" then the rename_commands object would be: { KEYS : "DO-NOT-USE" } .
tls_optnullAn object containing options to pass to net to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel).
prefixnullA string used to prefix all used keys (e.g. namespace:test). Please be aware that the keys command will not be prefixed. The keys command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed.
retry_strategyfunctionA function that receives an options object as parameter including the retry attempt, the total_retry_time indicating how much time passed since the last time connected, the error why the connection was lost and the number of times_connected in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below.

Example

  • detect_buffers example:
const iosched = require('iosched');
const redis = require('redis');

const client = redis.createClient({ host: '10.4.0.180', port: 6379, detect_buffers: true });

client.set('foo_rand000000000000', 'OK');

// This will return a JavaScript String
client.get('foo_rand000000000000', function (err, reply) {
  console.log(typeof reply + ':' + reply.toString());
});

// This will return a Buffer since original key is specified as a Buffer
client.get(new Buffer('foo_rand000000000000'), function (err, reply) {
  console.log(typeof reply + ':' + reply.toString());
});

while (true) {
  iosched.poll();
}
  • retry_strategy example:
const client = redis.createClient({
  host: '10.4.0.180',
  port: 6379,
  retry_strategy: function (options) {
    if (options.error && options.error.code === 'ECONNREFUSED') {
      // End reconnecting on a specific error and flush all commands with
      // a individual error
      return new Error('The server refused the connection');
    }
    if (options.total_retry_time > 1000 * 60 * 60) {
      // End reconnecting after a specific timeout and flush all commands
      // with a individual error
      return new Error('Retry time exhausted');
    }
    if (options.attempt > 10) {
      // End reconnecting with built in error
      return undefined;
    }
    // reconnect after
    return Math.min(options.attempt * 100, 3000);
  },
});

redis.print()

A handy callback function for displaying return values when testing. Example:

Example

const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.on('connect', function() {
  client.set('foo', 'bar', redis.print); // => 'Reply: OK'
  client.get('foo', redis.print); // => 'Reply: bar'
  client.quit();
});

while (true) {
  iosched.poll();
}

redis.add_command(command_name)

  • command_name {String} Command name.

Calling add_command will add a new command to the prototype. The exact command name will be used when calling using this new command. Using arbitrary arguments is possible as with any other command.

Redis Object

client.auth(password[, callback])

  • password {String} Passwrod.
  • callback {Function} Callback function.
    • error {Error} Error object.

When connecting to a Redis server that requires authentication, the AUTH command must be sent as the first command after connecting. This can be tricky to coordinate with reconnections, the ready check, etc. To make this easier, client.auth() stashes password and will send it after each connection, including reconnections. callback is invoked only once, after the response to the very first AUTH command sent.

client.quit(callback)

  • callback {Function} Callback function.
    • error {Error} Error object.

This sends the quit command to the redis server and ends cleanly right after all running commands were properly handled. If this is called while reconnecting (and therefore no connection to the redis server exists) it is going to end the connection right away instead of resulting in further reconnections! All offline commands are going to be flushed with an error in that case.

client.end(flush)

  • flush {Boolean} End robustly or not.

Forcibly close the connection to the Redis server. That this does not wait until all replies have been parsed. If you want to exit cleanly, call client.quit() as mentioned above.

You should set flush to true, if you are not absolutely sure you do not care about any other commands. If you set flush to false all still running commands will silently fail.

Example

This example closes the connection to the Redis server before the replies have been read. You probably don't want to do this:

const iosched = require('iosched');
const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.set('hello', 'world', function (err) {
  // This will either result in an error (flush parameter is set to true)
  // or will silently fail and this callback will not be called at all (flush set to false)
  console.error(err);
});

// No further commands will be processed
client.end(true);

client.get('hello', function (err) {
  console.error(err); // => 'The connection has already been closed.'
});

while (true) {
  iosched.poll();
}

client.end() without the flush parameter set to true should NOT be used in production!

Redis Events

client will emit some events about the state of the connection to the Redis server.

ready

client will emit ready once a connection is established. Commands issued before the ready event are queued, then replayed just before this event is emitted.

connect

client will emit connect as soon as the stream is connected to the server.

reconnecting

client will emit reconnecting when trying to reconnect to the Redis server after losing the connection. Listeners are passed an object:

  • params {Object}
    • delay {Integer} In ms from the previous try.
    • attempt {Integer} The attempt times attributes.

error

client will emit error when encountering an error connecting to the Redis server or when any other in Redis occurs. If you use a command without callback and encounter a ReplyError it is going to be emitted to the error listener. So please attach the error listener to Redis.

end

client will emit end when an established Redis server connection has closed.

warning

client will emit warning when password was set but none is needed and if a deprecated option / function / similar is used.

Error Handling

Currently the following Error subclasses exist:

  • RedisError: All errors returned by the client.
  • ReplyError subclass of RedisError: All errors returned by Redis itself.
  • AbortError subclass of RedisError: All commands that could not finish due to what ever reason.
  • ParserError subclass of RedisError: Returned in case of a parser error (this should not happen).
  • AggregateError subclass of AbortError: Emitted in case multiple unresolved commands without callback got rejected in debug_mode instead of lots of AbortErrors.

All error classes are exported by the module.

Example

const iosched = require('iosched');
const assert = require('assert');
const redis = require('redis');
const { AbortError, AggregateError, ReplyError } = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.on('error', function (err) {
  assert.assert(err instanceof Error);
  assert.assert(err instanceof AbortError);
  assert.assert(err instanceof AggregateError);

  // The set and get are aggregated in here
  assert.strictEqual(err.errors.length, 2);
  assert.strictEqual(err.code, 'NR_CLOSED');
});

client.set('foo', 'bar', 'baz', function (err, res) {
  // Too many arguments
  assert.assert(err instanceof ReplyError); // => true
  assert.strictEqual(err.command, 'SET');
  console.log(err.args);

  redis.debug_mode = true;

  client.set('foo', 'bar');
  client.get('foo');

  Task.nextTick(function () {
    // Force closing the connection while the command did not yet return
    client.end(true);
    redis.debug_mode = false;
  });
});

while (true) {
  iosched.poll();
}

Every ReplyError contains the command name in all-caps and the arguments (args).

If Redis emits a library error because of another error, the triggering error is added to the returned error as origin attribute.

Error codes: Redis returns a NR_CLOSED error code if the clients connection dropped. If a command unresolved command got rejected a UNCERTAIN_STATE code is returned. A CONNECTION_BROKEN error code is used in case Redis gives up to reconnect.

Redis Commands

Hash Commands

Most Redis commands take a single String or an Array of Strings as arguments, and replies are sent back as a single String or an Array of Strings. When dealing with hash values, there are a couple of useful exceptions to this.

client.hmset(hash, key1, val1, ...keyN, valN[, callback])

  • hash {String} Redis key.
  • key {String} Hash field key.
  • val {String | Buffer | Number | Date} Hash field value.
  • callback {Function} Callback function.
    • error {Error} Error object.

Multiple values may also be set by supplying more arguments.

client.hmset(hash, field1, ...fieldN[, callback])

  • hash {String} Redis key.
  • field {Object} Redis hash field object, as: {key: val}.
    • key {String} Hash field key.
    • val {String | Buffer | Number | Date} Hash field value.
  • callback {Function} Callback function.
    • error {Error} Error object.

The same as client.hmset(hash, key1, val1, ...keyN, valN[, callback]).

client.hmset(hash, fields[, callback])

  • hash {String} Redis key.
  • fields {Array} Redis hash field object array, as: {key1: val1}, ...{keyN: valN}.
  • callback {Function} Callback function.
    • error {Error} Error object.

The same as client.hmset(hash, key1, val1, ...keyN, valN[, callback]).

client.hgetall(hash[, callback])

  • hash {String} Redis key.
  • callback {Function} Callback function.
    • error {Error} Error object.
    • value {Object} Get result.

The reply from an HGETALL command will be converted into a JavaScript Object. That way you can interact with the responses using JavaScript syntax.

Example

const iosched = require('iosched');
const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.hmset('key', 'foo', 'bar', 'hello', 'world');

client.hgetall('key', function (err, value) {
  console.log(value.foo); // > 'bar'
  console.log(value.hello); // > 'world'
});

while (true) {
  iosched.poll();
}

Publish & Subscribe

client.publish(channel, message)

  • channel {String} Publish channel.
  • message {String | Number | Buffer | Date} Publish message.

client.subscribe(channel)

  • channel {String} Subscribe channel.

client.unsubscribe(channel)

  • channel {String} Unsubscribe channel.

When a client issues a SUBSCRIBE or PSUBSCRIBE, that connection is put into a "subscriber" mode. At that point, the only valid commands are those that modify the subscription set, and quit (also ping on some redis versions). When the subscription set is empty, the connection is put back into regular mode.

If you need to send regular commands to Redis while in subscriber mode, just open another connection with a new client (use client.duplicate() to quickly duplicate an existing client).

Subscriber Events

If a client has subscriptions active, it may emit these events:

message

  • channel {String} Channel.
  • message {String} Message.

Client will emit message for every message received that matches an active subscription. Listeners are passed the channel name as channel and the message as message.

message_buffer

  • channel {String} Channel.
  • message {Buffer} Message.

This is the same as the message event with the exception, that it is always going to emit a buffer. If you listen to the message event at the same time as the message_buffer, message event is always going to emit a string.

subscribe

  • channel {String} Channel.
  • count {Integer} The new count of subscriptions for this client.

Client will emit subscribe in response to a SUBSCRIBE command. Listeners are passed the channel name as channel and the new count of subscriptions for this client as count.

unsubscribe

  • channel {String} Channel.
  • count {Integer} The new count of subscriptions for this client as count.

Client will emit unsubscribe in response to a UNSUBSCRIBE command. Listeners are passed the channel name as channel and the new count of subscriptions for this client as count. When count is 0, this client has left subscriber mode and no more subscriber events will be emitted.

pmessage

  • pattern {String} Pattern string.
  • channel {String} Channel.
  • message {String} Message.

Client will emit pmessage for every message received that matches an active subscription pattern. Listeners are passed the original pattern used with PSUBSCRIBE as pattern, the sending channel name as channel, and the message as message.

pmessage_buffer

  • pattern {String} Pattern string.
  • channel {String} Channel.
  • message {Buffer} Message.

This is the same as the pmessage event with the exception, that it is always going to emit a buffer. If you listen to the pmessage event at the same time as the pmessage_buffer, pmessage event is always going to emit a string.

psubscribe

  • pattern {String} Pattern string.
  • count {Integer} The new count of subscriptions for this client.

Client will emit psubscribe in response to a PSUBSCRIBE command. Listeners are passed the original pattern as pattern, and the new count of subscriptions for this client as count.

punsubscribe

  • pattern {String} Pattern string..
  • count {Integer} The new count of subscriptions for this client.

Client will emit punsubscribe in response to a PUNSUBSCRIBE command. Listeners are passed the channel name as channel and the new count of subscriptions for this client as count. When count is 0, this client has left subscriber mode and no more subscriber events will be emitted.

Example

This example opens two client connections, subscribes to a channel on one of them, and publishes to that channel on the other.

const iosched = require('iosched');
const redis = require('redis');

const subscriber = redis.createClient({ host: '10.4.0.180', port: 6379 });
const publisher = redis.createClient({ host: '10.4.0.180', port: 6379 });

let messageCount = 0;

subscriber.on('subscribe', function (channel, count) {
  publisher.publish('a channel', 'a message');
  publisher.publish('a channel', 'another message');
});

subscriber.on('message', function (channel, message) {
  messageCount += 1;
  console.log("Subscriber received message in channel '" + channel + "': " + message);

  if (messageCount === 2) {
    subscriber.unsubscribe();
    subscriber.quit();
    publisher.quit();
  }
});

subscriber.subscribe('a channel');

while (true) {
  iosched.poll();
}

Multi-Execute

client.multi()

  • returns {Multi} Multi object.

MULTI commands are queued up until an EXEC is issued, and then all commands are run atomically by Redis. The interface returns an individual Multi object by calling client.multi(). If any command fails to queue, all commands are rolled back and none is going to be executed.

Example

const iosched = require('iosched');
const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

let setSize = 20;

client.sadd('key_multi', 'member1');
client.sadd('key_multi', 'member2');

while (setSize > 0) {
  client.sadd('key_multi', 'member' + setSize);
  setSize -= 1;
}

// chain commands
client
  .multi()
  .scard('key_multi')
  .smembers('key_multi')
  .keys('*')
  .dbsize()
  .exec(function (err, replies) {
    console.log('MULTI got ' + replies.length + ' replies');
    replies.forEach(function (reply, index) {
    console.log('REPLY  @ index ' + index + ': ' + reply.toString());
    });
  });

while (true) {
  iosched.poll();
}

Multi.exec([callback])

  • callback {Function} Callback.

client.multi() is a constructor that returns a Multi object. Multi objects share all of the same command methods as client objects do. Commands are queued up inside the Multi object until Multi.exec() is invoked.

If your code contains an syntax error an EXECABORT error is going to be thrown and all commands are going to be aborted. That error contains a .errors property that contains the concrete errors. If all commands were queued successfully and an error is thrown by redis while processing the commands that error is going to be returned in the result array! No other command is going to be aborted though than the ones failing.

You can either chain together MULTI commands as in the above example, or you can queue individual commands while still sending regular client command as in this example:

Example

const iosched = require('iosched');
const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

// start a separate multi command queue
const multi = client.multi();

// add some commands to the queue
multi.incr('count_cats', redis.print);
multi.incr('count_dogs', redis.print);

// runs a command immediately outside of the `multi` instance
client.mset('count_cats', 100, 'count_dogs', 50, redis.print);

// drains the multi queue and runs each command atomically
multi.exec(function(err, replies) {
  console.log(replies); // 101, 51
});

while (true) {
  iosched.poll();
}

In addition to adding commands to the MULTI queue individually, you can also pass an array of commands and arguments to the constructor:

const iosched = require('iosched');
const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client
  .multi([
    ['mget', 'count_cats', 'count_dogs', redis.print],
    ['incr', 'count_birds'],
  ])
  .exec(function (err, replies) {
    console.log(replies);
  });

while (true) {
  iosched.poll();
}

Multi.exec_atomic([callback])

  • callback {Function} Callback.

Identical to Multi.exec but with the difference that executing a single command will not use transactions.

client.batch([commands])

  • commands {Array} Commands array.

Identical to .multi() without transactions. This is recommended if you want to execute many commands at once but don't need to rely on transactions.

BATCH commands are queued up until an EXEC is issued, and then all commands are run atomically by Redis. The interface returns an individual Batch object by calling client.batch(). The only difference between .batch and .multi is that no transaction is going to be used. Be aware that the errors are - just like in multi statements - in the result. Otherwise both, errors and results could be returned at the same time.

If you fire many commands at once this is going to boost the execution speed significantly compared to firing the same commands in a loop without waiting for the result! See the benchmarks for further comparison. Please remember that all commands are kept in memory until they are fired.

Optimistic Locks

Using multi you can make sure your modifications run as a transaction, but you can't be sure you got there first. What if another client modified a key while you were working with it's data?

To solve this, Redis supports the WATCH command, which is meant to be used with MULTI:

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.watch('foo', function (watchError) {
  if (watchError) throw watchError;

  client.get('foo', function (getError, result) {
    if (getError) throw getError;

    // Process result
    // Heavy and time consuming operation here to generate 'bar'
    client
      .multi()
      .set('foo', 'bar')
      .exec(function (execError, results) {
        /**
        * If err is null, it means Redis successfully attempted
        * the operation.
        */
        if (execError) throw err;

        /**
        * If results === null, it means that a concurrent client
        * changed the key while we were processing it and thus
        * the execution of the MULTI command was not performed.
        *
        * NOTICE: Failing an execution of MULTI is not considered
        * an error. So you will have err === null and results === null
        */
      });
  });
});

The above snippet shows the correct usage of watch with multi. Every time a watched key is changed before the execution of a multi command, the execution will return null. On a normal situation, the execution will return an array of values with the results of the operations.

As stated in the snippet, failing the execution of a multi command being watched is not considered an error. The execution may return an error if, for example, the client cannot connect to Redis.

An example where we can see the execution of a multi command fail is as follows:

const iosched = require('iosched');
const redis = require('redis');

const clients = {
  watcher: redis.createClient({ host: '10.4.0.180', port: 6379 }),
  modifier: redis.createClient({ host: '10.4.0.180', port: 6379 })
};

clients.watcher.watch('foo', function (watchError) {
  if (watchError) throw watchError;

  // if you comment out the next line, the transaction will work
  clients.modifier.set('foo', Math.random(), setError => {
    if (setError) throw err;
  });

  // using a setTimeout here to ensure that the MULTI/EXEC will come after the SET.
  // Normally, you would use a callback to ensure order, but I want the above SET command
  // to be easily comment-out-able.
  setTimeout(function () {
    clients.watcher
      .multi()
      .set('foo', 'bar')
      .set('hello', 'world')
      .exec((multiExecError, results) => {
        if (multiExecError) throw multiExecError;

        if (results === null) {
          console.log('transaction aborted because results were null');
        } else {
          console.log('transaction worked and returned', results);
        }

        clients.watcher.quit();
        clients.modifier.quit();
      });
  }, 1000);
});

while (true) {
  iosched.poll();
}

WATCH limitations

Redis WATCH works only on whole key values. For example, with WATCH you can watch a hash for modifications, but you cannot watch a specific field of a hash.

The following example would watch the keys foo and hello, not the field hello of hash foo:

const redis = require('redis');

const client = redis.createClient();

client.hget('foo', 'hello', function(hashGetError, result) {
  if (hashGetError) throw hashGetError;

  //Do some processing with the value from this field and watch it after
  client.watch('foo', 'hello', function(watchError) {
    if (watchError) throw watchError;

    /**
    * This is now watching the keys 'foo' and 'hello'. It is not
    * watching the field 'hello' of hash 'foo'. Because the key 'foo'
    * refers to a hash, this command is now watching the entire hash
    * for modifications.
    */
  });
});

This limitation also applies to sets (you can not watch individual set members) and any other collections.

Monitor Mode

client.monitor(callback)

  • callback {Function} Callback.
    • error {Error} Error object.
    • result {String} Always 'OK'.

Redis supports the MONITOR command, which lets you see all commands received by the Redis server across all client connections, including from other client libraries and other computers.

A monitor event is going to be emitted for every command fired from any client connected to the server including the monitoring client itself. The callback for the monitor event takes a timestamp from the Redis server, an array of command arguments and the raw monitoring string.

Example

const iosched = require('iosched');
const redis = require('redis');

var client = redis.createClient({ host: '10.4.0.180', port: 6379 });

client.monitor(function(err, res) {
  console.log('Entering monitoring mode.');
});

client.set('foo', 'bar');

client.on('monitor', function(time, args, rawReply) {
  console.log(time + ': ' + args); // 1458910076.446514:['set', 'foo', 'bar']
});

while (true) {
  iosched.poll();
}

Extras

Some other things you might find useful.

client.server_info

After the ready probe completes, the results from the INFO command are saved in the client.server_info object.

The versions key contains an array of the elements of the version string for easy comparison.

> client.server_info.redis_version
'2.3.0'
> client.server_info.versions
[ 2, 3, 0 ]

Multi-word commands

To execute redis multi-word commands like SCRIPT LOAD or CLIENT LIST pass the second word as first parameter:

client.script('load', 'return 1');

client
  .multi()
  .script('load', 'return 1')
  .exec();

client.multi([['script', 'load', 'return 1']]).exec();

client.duplicate([[options], callback])

  • options* {Object}* Options.
  • callback {Function} Callback.
    • error {Error} Error object.
  • returns: {Redis}

Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option. If you pass a callback, duplicate is going to wait until the client is ready and returns it in the callback. If an error occurs in the meanwhile, that is going to return an error instead in the callback.

One example of when to use duplicate() would be to accommodate the connection-blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands are used on the same Redis client instance as non blocking commands, the non-blocking ones may be queued up until after the blocking ones finish.

Another reason to use duplicate() is when multiple DBs on the same server are accessed via the redis SELECT command. Each DB could use its own connection.

client.send_command(command_name[, [args][, callback]])

  • command_name {String} Command name.
  • args {Array} Arguments.
  • callback [Function] Callback.

All Redis commands have been added to the client object. However, if new commands are introduced before this library is updated or if you want to add individual commands you can use send_command() to send arbitrary commands to Redis.

All commands are sent as multi-bulk commands. args can either be an Array of arguments, or omitted / set to undefined.

client.connected

  • {Boolean}

Boolean tracking the state of the connection to the Redis server.

client.command_queue_length

  • {Integer}

The number of commands that have been sent to the Redis server but not yet replied to. You can use this to enforce some kind of maximum queue depth for commands while connected.

client.offline_queue_length

  • {Integer}

The number of commands that have been queued up for a future connection. You can use this to enforce some kind of maximum queue depth for pre-connection commands.

文档内容是否对您有所帮助?
有帮助
没帮助